package org.example.ai.tools;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import lombok.RequiredArgsConstructor;
import org.example.ai.entity.po.Course;
import org.example.ai.entity.po.CourseReservation;
import org.example.ai.entity.po.School;
import org.example.ai.query.CourseQuery;
import org.example.ai.service.ICourseReservationService;
import org.example.ai.service.ICourseService;
import org.example.ai.service.ISchoolService;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class CourseTools {

    private final ICourseService courseService;
    private final ICourseReservationService courseReservationService;
    private final ISchoolService schoolService;
    @Tool(description = "查询课程信息")
    public List<Course> queryCourse(@ToolParam(description = "查询的条件",required = false) CourseQuery courseQuery) {
        if (courseQuery == null) {
            return List.of();
        }
        QueryChainWrapper<Course> apper = courseService.query()
                .eq(courseQuery.getType() != null, "type", courseQuery.getType())//type=?
                .le(courseQuery.getEdu() != null, "edu", courseQuery.getEdu());//edu<=?
        if (courseQuery.getSorts() != null&&!courseQuery.getSorts().isEmpty()) {
            courseQuery.getSorts().forEach(sort -> {
                apper.orderBy(true,sort.getAsc(),sort.getField());
            } );
        }
        return apper.list();
    }
    @Tool(description = "查询所有校区")
    public List<School> querySchool() {
        return schoolService.list();
    }
    @Tool(description = "生成课程预约单,并返回生成的预约单号")
    public Integer queryCourseCount(@ToolParam(description = "预约课程") String course,
                                    @ToolParam(description = "预约校区") String school,
                                    @ToolParam(description = "学生姓名") String studentName,
                                    @ToolParam(description = "联系电话") String contactInfo,
                                    @ToolParam(description = "备注",required = false) String remark){
        CourseReservation courseReservation = new CourseReservation();
        courseReservation.setCourse(course);
        courseReservation.setSchool(school);
        courseReservation.setStudentName(studentName);
        courseReservation.setContactInfo(contactInfo);
        courseReservation.setRemark(remark);
        courseReservationService.save(courseReservation);
        return courseReservation.getId();
    }
}
